//
//  ImageBrowseViewCell.m
//  etionUI
//
//  Created by wangjian on 1/28/14.
//  Copyright (c) 2014 GuangZhouXuanWu. All rights reserved.
//

#import "ImageBrowserViewCell.h"

#define PROGRESS_VIEW_SIZE 60

@implementation CImageBrowserViewCellInfo

- (void)dealloc
{
    [_image release];
    [super dealloc];
}

@end

@interface CImageBrowserViewCell() <UIScrollViewDelegate>

@property (nonatomic, assign) UIScrollView *scrollView;
@property (nonatomic, assign) UIImageView *imageView;

@end

@implementation CImageBrowserViewCell
{
    UIActivityIndicatorView *_progressView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)] autorelease];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.delegate = self;
        [self.contentView addSubview:self.scrollView];
        
        self.imageView = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
        [self.scrollView addSubview:self.imageView];
    }
    return self;
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    
    self.scrollView.zoomScale = 1.0;
    self.imageView.image = nil;
}

- (UIActivityIndicatorView *)progressView
{
    if (_progressView == nil)
    {
        _progressView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
        _progressView.center = self.scrollView.center;
        [self addSubview:_progressView];
    }
    return _progressView;
}

- (UIImage *)image
{
    return self.imageView.image;
}

- (void)setImage:(UIImage *)image
{
    self.imageView.image = image;
    if (image != nil)
    {
        CGFloat viewW = 0, viewH = 0;
        CGFloat mul = [UIImage caleFitImageSize:image.size targetSize:self.scrollView.size width:&viewW height:&viewH];
        if (mul < 1)
        {
            self.scrollView.maximumZoomScale = 1 / mul;
        }
        else if (mul > 1)
        {
            self.scrollView.minimumZoomScale = 1 / mul;
        }
        if (self.scrollView.maximumZoomScale < 4)
        {
            self.scrollView.maximumZoomScale = 4;
        }
        UIEdgeInsets anEdgeInset = { (self.scrollView.size.height - viewH) / 2, (self.scrollView.size.width - viewW) / 2, (self.scrollView.size.height - viewH) / 2, (self.scrollView.size.width - viewW) / 2};
        self.scrollView.contentInset = anEdgeInset;
        self.scrollView.contentSize = CGSizeMake(viewW, viewH);
        self.scrollView.contentOffset = CGPointMake(-anEdgeInset.left, -anEdgeInset.top);
        self.imageView.frame = CGRectMake(0, 0, viewW, viewH);
    }
    [_progressView stopAnimating];
}

- (void)updateWithCellInfo:(id)cellInfo
{
    CImageBrowserViewCellInfo *info = cellInfo;
    if (info.processing == YES)
    {
        [self.processView startAnimating];
    }
    else
    {
        [self.processView stopAnimating];
    }
    self.image = info.image;
}

#pragma mark - animated show or hide

- (CGRect)frameOfImage
{
    return CGRectOffset(self.imageView.frame, self.scrollView.contentInset.left, self.scrollView.contentInset.top);
}

- (void)setImageZoomScale:(CGFloat)zoomScale animated:(BOOL)animated complete:(void (^)())complete
{
    if (self.scrollView.zoomScale != zoomScale)
    {
        [self.scrollView setZoomScale:zoomScale animated:animated];
        [self adjustImageViewFrameComplete:^{
            complete();
        }];
    }
    else
    {
        complete();
    }
}

- (void)adjustImageViewFrameComplete:(void (^)())complete
{
    [UIView animateWithDuration:.25 animations:^
    {
        CGRect innerFrame = self.imageView.frame;
        CGRect scrollerBounds = self.scrollView.bounds;
        
        if ( ( innerFrame.size.width < scrollerBounds.size.width ) || ( innerFrame.size.height < scrollerBounds.size.height ) )
        {
            CGFloat tempx = self.imageView.center.x - (scrollerBounds.size.width / 2 );
            CGFloat tempy = self.imageView.center.y - (scrollerBounds.size.height / 2 );
            CGPoint myScrollViewOffset = CGPointMake( tempx, tempy);
            
            self.scrollView.contentOffset = myScrollViewOffset;
        }
        
        UIEdgeInsets anEdgeInset = { 0, 0, 0, 0};
        if ( scrollerBounds.size.width > innerFrame.size.width )
        {
            anEdgeInset.left = (scrollerBounds.size.width - innerFrame.size.width) / 2;
            anEdgeInset.right = -anEdgeInset.left;
        }
        if ( scrollerBounds.size.height > innerFrame.size.height )
        {
            anEdgeInset.top = (scrollerBounds.size.height - innerFrame.size.height) / 2;
            anEdgeInset.bottom = -anEdgeInset.top;
        }
        self.scrollView.contentInset = anEdgeInset;
    } completion:^(BOOL finished)
    {
        complete();
    }];
}

#pragma mark - scroll view delegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    [self adjustImageViewFrameComplete:^{
        
    }];
}
@end
